package com.pp.tourism.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pp.tourism.web.dto.ApiResponse;
import com.pp.tourism.web.pojo.User;
import com.pp.tourism.web.pojo.UserCommentNode;
import com.pp.tourism.web.pojo.UserCommentTop;
import com.pp.tourism.web.service.UserCommentNodeService;
import com.pp.tourism.web.service.UserCommentTopService;
import com.pp.tourism.web.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 用户景点点评表 前端控制器
 * </p>
 *
 * @author wjm
 * @since 2019-03-20
 */
@RestController
@RequestMapping("/userCommentTop")
public class UserCommentTopController {
    @Autowired
    private UserCommentTopService userCommentTopService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserCommentNodeService userCommentNodeService;
    @Autowired
    private UserCommentTopService userCommentTop;

    /**
     * wjm
     */
    @ApiOperation("新增点评")
    @PostMapping("/commentTop")
    public ApiResponse commentTop(@RequestBody Map<String, Object> param) {
        try {
            String username = param.get("username").toString();
            //获取用户id  commentImgUrl
            User user = userService.queryUserByName(username);

            UserCommentTop userCommentTop = new UserCommentTop();
            userCommentTop.setUserId(user.getId());
            userCommentTop.setComment( param.get("comment").toString());
            userCommentTop.setScenicId(Integer.valueOf(param.get("scenicId").toString()));
            if(param.get("commentImgUrl").toString()!=null){
                userCommentTop.setCommentImgUrl(param.get("commentImgUrl").toString());
            }
            boolean insert = userCommentTopService.insert(userCommentTop);

            return ApiResponse.ok(insert);
        } catch (IllegalArgumentException e){
            e.printStackTrace();
            return ApiResponse.error("500", e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return ApiResponse.error("500", "操作失败！");
        }
    }

    /**
     * wjm
     * 获取最新评论信息
     */
    @ApiOperation("获取所有评论信息")
    @PutMapping("/getCommentTop")
    public ApiResponse getCommentTop(@RequestBody String id) {
        try{
            //所有用户信息
            List<User> userlist = userService.selectList(new EntityWrapper<User>());

            //获取一级评论信息
            HashMap<String, Object> map = new HashMap<>();
            map.put("scenic_Id",id);
            List<UserCommentTop> userCommentTops = userCommentTopService.selectByMap(map);
            //获取二级评论
            List<UserCommentNode> userCommentNodes = userCommentNodeService.selectByMap(map);

            //获取用户名
            List<String> userid = new ArrayList<>();
            for (UserCommentTop s:userCommentTops) {
                userid.add(s.getUserId().toString());
            }
            List<User> users = userService.selectBatchIds(userid);

            //数据拼接
            Map<String, Object> unt = new HashMap<>();
            int ss=0;
            for (UserCommentTop uct:userCommentTops) {
                Map<String, Object> data = new HashMap<>();
               //一级数据封装
               for(int i=0;i<users.size();i++){
                    if(uct.getUserId().equals(users.get(i).getId())){
                        data.put("username",users.get(i).getUsername());
                        data.put("userId",uct.getUserId());
                        data.put("scenicId",uct.getScenicId());
                        data.put("comment",uct.getComment());
                        data.put("commentImgUrl",uct.getCommentImgUrl());
                        data.put("topid",uct.getId());
                    }
                }
                //二级封装
                Map<String, Object> ucnomcount = new HashMap<>();
                int sss=0;
                for(int j=0;j<userCommentNodes.size();j++){
                    if(uct.getUserId().equals(userCommentNodes.get(j).getMainUserId()) && uct.getId().equals(userCommentNodes.get(j).getCommentTopId())){
                        Map<String, Object> ucnom = new HashMap<>();
                        ucnom.put("node_comment",userCommentNodes.get(j).getComment());
                        ucnom.put("node_scenicId",userCommentNodes.get(j).getScenicId());
                        for(int k=0;k<userlist.size();k++){
                            if(userlist.get(k).getId().equals(userCommentNodes.get(j).getMainUserId())){
                                ucnom.put("node_mainUserId",userlist.get(k).getUsername());
                            }
                            if(userlist.get(k).getId().equals(userCommentNodes.get(j).getReplyUserId())){
                                ucnom.put("node_replyUserId",userlist.get(k).getUsername());
                            }
                        }
                        ucnom.put("node_id",userCommentNodes.get(j).getId());
                        ucnomcount.put(sss+"",ucnom);
                        sss++;
                    }
                }
               /* for (Object sa:ucnomcount.values()) {
                    System.out.println(sa);
                }*/
                data.put("countucnom",ucnomcount);
                unt.put(ss+"",data);
                ss++;
            }

            return ApiResponse.ok(unt);
         } catch (Exception e) {
             e.printStackTrace();
             return ApiResponse.error("500", "查询失败！");
         }
    }
    //list列表去重
    public static List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

}

